﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace PHMEE
{
    public class LanguageHeaderOperationFilterPHMEE : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            operation.Parameters ??= new List<OpenApiParameter>();

            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "Accept-Language",
                In = ParameterLocation.Header,
                Required = false,
                Description = "Language setting: zh-Hans or en. Default is zh-Hans.",
                Schema = new OpenApiSchema { Type = "string" }
            });
        }
    }

    public class VesselHeaderOperationFilterPHMEE : IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext context)
        {
            
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            if (context.HttpContext.Request.Headers.TryGetValue("Vessel-Id", out var vesselId))
            {
                if(Guid.TryParse(vesselId,out Guid vesselIdGuid))
                {
                    // 添加到 Action 参数中
                    context.ActionArguments["VesselId"] = vesselIdGuid;
                    context.ActionArguments["vesselId"] = vesselIdGuid;
                }
            }
            if(context.HttpContext.Request.Headers.TryGetValue("On-Vessel", out var onVessel))
            {
                if(bool.TryParse(onVessel, out bool onVesselBool))
                {
                    // 添加到 Action 参数中
                    context.ActionArguments["OnVessel"] = onVesselBool;
                    context.ActionArguments["onVessel"] = onVesselBool;
                }
            }
        }
    }
}
